package com.dhcc.cdc.sys.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.entity.CdcProductEntity;
import com.dhcc.cdc.sys.service.ICdcProductService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 产品线
 * 
 * @ClassName: CdcProductController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:18:51
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcproduct")
public class CdcProductController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcproduct/";

	@Autowired
	private ICdcProductService cdcProductService;
	

    /**
     * 跳转到产品线列表首页
     */
    @RequestMapping("")
    @Permission("cdcproduct:list")
    public String index() {
        return PREFIX + "cdcproduct.html";
    }
	
    /**
     * 跳转到新增产品线
     */
    @RequestMapping("/to_add")
    @Permission("cdcproduct:add")
    public String toAdd() {
        return PREFIX + "cdcproduct_add.html";
    }
	
	/**
     * 跳转到修改产品线
     */
    @RequestMapping("/to_update/{cdcProductId}")
    @Permission("cdcproduct:update")
    public String toUpdate(@PathVariable Long cdcProductId, Model model) {
        CdcProductEntity cdcProduct = this.cdcProductService.selectById(cdcProductId);
        model.addAttribute("cdcProduct",cdcProduct);
        return PREFIX + "cdcproduct_edit.html";
    }

    /**
     * 获取产品线列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcproduct:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcProductEntity> page = new PageFactory<CdcProductEntity>().defaultPage();
        List<CdcProductEntity> result = this.cdcProductService.page(page,params);
        page.setRecords((List<CdcProductEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增产品线
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcproduct:add")
    @BussinessLog(value = "新增产品线")
    public Object add(CdcProductEntity cdcProduct) {
        if (CommonUtil.isOneEmpty(cdcProduct)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcProduct.insert();
        return R.ok();
    }

    /**
     * 删除产品线
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcproduct:delete")
    @BussinessLog(value = "删除产品线")
    public Object delete(@RequestParam Long cdcProductId) {
	
        this.cdcProductService.deleteById(cdcProductId);

        return R.ok();
    }

    /**
     * 修改产品线
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcproduct:update")
    @BussinessLog(value = "修改产品线")
    public Object update(CdcProductEntity cdcProduct) {
        if (CommonUtil.isOneEmpty(cdcProduct)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcProductEntity old = this.cdcProductService.selectById(cdcProduct.getId());

        old.setCode(cdcProduct.getCode());
        old.setDescription(cdcProduct.getDescription());
        old.setVersion(cdcProduct.getVersion());
        old.setOrganizationId(cdcProduct.getOrganizationId());
        old.setIsInputData(cdcProduct.getIsInputData());
        old.setIsOutputData(cdcProduct.getIsOutputData());
        old.setIsHandleData(cdcProduct.getIsHandleData());
        old.setIsActive(cdcProduct.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
    @RequestMapping(value = {"/query/","/query/{productCode}"})
    @Permission("dept:list")
    @ResponseBody
    public Object query(@RequestParam Map<String, Object> params,@PathVariable(required = false) String productCode) {
        if(productCode != null) params.put("code", productCode);
        List<CdcProductEntity> result = this.cdcProductService.list(params);
    	return result;
    }
}
